package com.mfm;

import java.util.*;
import java.util.stream.Stream;

/**
 * Stream中间操作
 * @Auther: MFM
 * @Date: 2022/3/1 - 03 - 01 - 14:28
 * @version: 1.0
 */

public class StreamTest1 {
    class student{
        int id;
        String name;
        int age;

        public student() {
        }

        public student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public student(int id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            student student = (student) o;
            return id == student.id && age == student.age && Objects.equals(name, student.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, age);
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }


    //筛选与切片
    public void test1(){
        List<student> list = new ArrayList<>();
        list.add(new student(1,"zhangsan",12));
        list.add(new student(2,"lisi",14));
        list.add(new student(3,"wangwu",10));
        list.add(new student(4,"maliu",9));
        list.add(new student(5,"huqi",11));
        list.add(new student(5,"huqi",11));

        //filter(Predicate p)--接收lambda，从流中排除某些元素。筛选指定条件字段后返回一个符合条件的对象的流
        list.stream().filter(s -> s.getAge() > 10).forEach(System.out::println);
        System.out.println();
        //limit(n)--截断流，使其元素不超过n
        list.stream().limit(3).forEach(System.out::println);
        System.out.println();
        //skip(n)--跳过元素，返回一个扔掉了前n个元素的流。若流中元素不大于n就会返回一个空流
        list.stream().skip(2).forEach(System.out::println);
        System.out.println();
        //distinct()--筛选，通过流所生成的元素的hashCode() 和 equals() 去除重复元素
        list.stream().distinct().forEach(System.out::println);
    }

    //映射
    public void test2(){
        List<student> list = new ArrayList<>();
        list.add(new student(1,"zhangsan",12));
        list.add(new student(2,"lisi",14));
        list.add(new student(3,"wangwu",10));
        list.add(new student(4,"maliu",9));
        list.add(new student(5,"huqi",11));
        list.add(new student(5,"huqi",11));
        //map(Function f)--接收一个函数作为参数，将元素转换成其他形式或提取信息，该函数会被应用到每一个元素上，并将其映射成一个新的元素
        List<String> list1 = Arrays.asList("aa","bb","cc");
        list1.stream().map(String::toUpperCase).forEach(System.out::println);
        System.out.println();
        //打印出长度大于5的名字
        list.stream().map(student::getName).filter(s -> s.length()>5).forEach(System.out::println);
        System.out.println();

        //flatMap(Function f)--接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流。
        //当流嵌套时（流中元素为流）可以用flatMap把流中流的元素取出来
        //不用flatMap取出元素的方法
        Stream<Stream<Character>> streamStream = list1.stream().map(StreamTest1::toStringStream);//该流里面有流
        streamStream.forEach(e -> e.forEach(System.out::println));
        //用flatMap取出元素
        Stream<Character> characterStream = list1.stream().flatMap(StreamTest1::toStringStream);
        characterStream.forEach(System.out::println);
    }

    public static Stream<Character> toStringStream(String str){
        ArrayList<Character> list = new ArrayList<>();
        for (Character c:str.toCharArray()) {
            list.add(c);
        }
        return list.stream();
    }

    //排序
    public void test3(){
        List<student> list = new ArrayList<>();
        list.add(new student(1,"zhangsan",12));
        list.add(new student(2,"lisi",14));
        list.add(new student(3,"wangwu",10));
        list.add(new student(4,"maliu",9));
        list.add(new student(5,"huqi",11));
        list.add(new student(5,"huqi",11));

        //sorted()--自然排序
        List<Integer> list1 = Arrays.asList(12,43,55,23,65,-1,-49,12);
        list1.stream().sorted().forEach(System.out::println);
        System.out.println();

        //自定义对象就需要重写Comparable否则就会报异常
//        list.stream().sorted().forEach(System.out::println);
//        System.out.println();

        //sorted(Comparator com)--定制排序
        list.stream().sorted((o1, o2) -> {
            if (o1.getId()>o2.getId()){
                return -1;
            }else {
                return 1;
            }
        }).forEach(System.out::println);
//        list.stream().sorted((o1, o2) -> Integer.compare(o1.getId(),o2.getId())).forEach(System.out::println);

    }

    public static void main(String[] args) {
        new StreamTest1().test3();
    }
}
